﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Web.UI;
using System.Text;

namespace MVCnExt.Web.UI.Ext
{
    public abstract class ExtReader : ExtConfig 
    {
        private List<ExtRecord> _fields = new List<ExtRecord>();

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            if (list.Count == 0)
                list.Add(new ExtScriptControlDescriptor("Ext.data.Record.create", "ctl00"));

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.data.Record.create";

            String[] properties = new String[]{
                "ID"     
            };

            Type component = typeof(ExtReader);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtReader.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }


            //add column model
            if (fields.Count != 0)
            {
                // build items list
                StringBuilder tabList = new StringBuilder("[");

                for (int i = 0; i < fields.Count; i++)
                {
                    // add comma between items
                    if (i != 0)
                    {
                        tabList.Append(",");
#if DEBUG
                        tabList.AppendLine("\t\t\t");
#endif
                    }

                    // get the item script
                    IList<ScriptDescriptor> descriptors = fields[i].GetScriptDescriptors();

                    // write the item's config to the list
                    tabList.Append(((ExtScriptControlDescriptor)descriptors[0]).WriteScript(ScriptType.ConfigOnly));
                }

                tabList.Append("]");

                desc.AddPropertyBase("fields", tabList);
            }

            return list;
        }

        #region Public Properties

        /// <summary>
        /// Name of the property within a row object that contains a record identifier value.
        /// </summary>
        [Description("Name of the property within a row object that contains a record identifier value.")]
        public String ID { get; set; }

        ///// <summary>
        ///// An array of field definition objects
        ///// </summary>                
        //[Description("Either a Collection of RecordField definition objects")]
        //[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        //[PersistenceMode(PersistenceMode.InnerProperty)]
        //[TemplateContainer(typeof(ExtContainer))]
        //[TemplateInstance(TemplateInstance.Single)]
        //public List<ExtRecord> fields { get; set; }
        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<ExtRecord> fields
        {
            get
            {
                return _fields;
            }
        }

        #endregion
    }
}
